package com.example.scenictourback.service.impl;

import com.example.scenictourback.dto.TicketDTO;
import com.example.scenictourback.entity.ScenicArea;
import com.example.scenictourback.entity.Ticket;
import com.example.scenictourback.repository.ScenicAreaRepository;
import com.example.scenictourback.repository.TicketRepository;
import com.example.scenictourback.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TicketServiceImpl implements TicketService {

    @Autowired
    private TicketRepository ticketRepository;
    
    @Autowired
    private ScenicAreaRepository scenicAreaRepository;

    @Override
    public List<TicketDTO> listAllTickets() {
        List<Ticket> tickets = ticketRepository.findAll();
        return TicketDTO.fromEntities(tickets);
    }

    @Override
    public List<TicketDTO> listTicketsByScenicAreaId(Integer scenicAreaId) {
        List<Ticket> tickets = ticketRepository.findByScenicAreaId(scenicAreaId);
        return TicketDTO.fromEntities(tickets);
    }

    @Override
    public TicketDTO getTicketById(Integer id) {
        Ticket ticket = ticketRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("门票不存在"));
        return TicketDTO.fromEntity(ticket);
    }

    @Override
    @Transactional
    public TicketDTO addTicket(Ticket ticket) {
        // 检查景区是否存在
        ScenicArea scenicArea = scenicAreaRepository.findById(ticket.getScenicArea().getId())
                .orElseThrow(() -> new RuntimeException("景区不存在"));
        
        ticket.setScenicArea(scenicArea);
        
        // 保存门票信息
        Ticket savedTicket = ticketRepository.save(ticket);
        return TicketDTO.fromEntity(savedTicket);
    }

    @Override
    @Transactional
    public TicketDTO updateTicket(Integer id, Ticket ticket) {
        // 检查门票是否存在
        if (!ticketRepository.existsById(id)) {
            throw new RuntimeException("门票不存在");
        }
        
        // 检查景区是否存在
        ScenicArea scenicArea = scenicAreaRepository.findById(ticket.getScenicArea().getId())
                .orElseThrow(() -> new RuntimeException("景区不存在"));
        
        ticket.setId(id);
        ticket.setScenicArea(scenicArea);
        
        // 更新门票信息
        Ticket updatedTicket = ticketRepository.save(ticket);
        return TicketDTO.fromEntity(updatedTicket);
    }

    @Override
    @Transactional
    public void deleteTicket(Integer id) {
        // 检查门票是否存在
        if (!ticketRepository.existsById(id)) {
            throw new RuntimeException("门票不存在");
        }
        
        // 删除门票
        ticketRepository.deleteById(id);
    }
    
    @Override
    @Transactional
    public TicketDTO updateTicketQuantity(Integer id, Integer totalQuantity, Integer availableQuantity) {
        // 检查门票是否存在
        Ticket ticket = ticketRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("门票不存在"));
        
        // 校验数量
        if (availableQuantity > totalQuantity) {
            throw new RuntimeException("可用数量不能大于总数量");
        }
        
        // 更新数量
        ticket.setTotalQuantity(totalQuantity);
        ticket.setAvailableQuantity(availableQuantity);
        
        // 保存更新
        Ticket updatedTicket = ticketRepository.save(ticket);
        return TicketDTO.fromEntity(updatedTicket);
    }
} 